//
//  AFNManger.m
//  Loan
//
//  Created by hejinhui on 2017/11/7.
//  Copyright © 2017年 hejinhui. All rights reserved.
//

#import "AFNManger.h"

@interface AFNManger()

@property (nonatomic, strong) AFHTTPSessionManager * manager;

@end

@implementation AFNManger

-(instancetype)init
{
    
    if (self = [super init]) {
        
        self.manager = [AFHTTPSessionManager manager];
        //申明返回的结果是json类型
        self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//          self.manager.requestSerializer = [AFHTTPRequestSerializer serializer];

        //申明请求的数据是json类型
        self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
        //如果报接受类型不一致请替换一致text/html或别的
        self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    }
    return self;
}

+(AFNManger *)shareRequsetMangager
{
    static   AFNManger *shareManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        shareManager = [[AFNManger alloc]init];
        
    });
    return shareManager;
}

-(void)requsetUrlType:(HttpRequestType)type
                  url:(NSString *)url
               params:(id)parmas
              successBlock:(void(^)(id result))success
            failBlock:(void(^)(NSError * error))fail
{
    if (type == HttpRequestTypeGet) {
        
        [self.manager GET:url parameters:parmas progress:^(NSProgress * _Nonnull downloadProgress) {
            
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if (success) {
                
                success(responseObject);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            if (fail) {
                
                fail(error);
            }
        }];
    }else if (type == HttpRequestTypePost)
    {
        [self.manager POST:url parameters:parmas progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if (success) {
                
                success(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            if (fail) {
                
                fail(error);
            }
        }];
        
    }
    
   
}

@end
